package com.bulletelc.apollopre.module.main.home;

import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.BannerBean;
import com.bulletelc.apollopre.entity.Classify;
import com.bulletelc.apollopre.entity.ProductDetail;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.bulletelc.apollopre.utils.LogUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/12
 * desc  : TODO
 */

public class HomePresenter extends BasePresenter<HomeContract.View> implements HomeContract.Presenter {
    private Gson gson;

    public HomePresenter() {
        gson = new Gson();
    }

    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public void getSliderShow() {
        getSliderShowImp();
    }

    @Override
    public void getPackage(int page) {
        getPackageImp(page);
    }

    @Override
    public void getHomeCate() {
        getHomeCateImp();
    }

    /**
     * 获取轮播图产品
     */
    private void getSliderShowImp() {
        OkGo.<String>get(NetWorkPath.GET_HOME_SLIDER_SHOW)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        System.out.println("----u-----"+response);
                        LogUtil.i(response.body().toString());
                        parseSliderShow(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        System.out.println("----y-----"+response);
                        super.onError(response);
                    }
                });
    }

    /**
     * 解析轮播产品数据
     */
    private void parseSliderShow(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject object = new JSONObject(response.body().toString());
                JSONObject meta = object.getJSONObject("meta");
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(meta.getString("code"))) {
                    JSONObject object1 = object.getJSONObject("data");
                    JSONArray object1JSONArray = object1.getJSONArray("image_url");
                    List<BannerBean> bannerBeanList = new ArrayList<>();
                    for (int i = 0; i < object1JSONArray.length(); i++) {
                        BannerBean bb = new BannerBean();
                        bb.setImageUrl(object1JSONArray.getJSONObject(i).getString("image_src"));
                        bb.setLink(object1JSONArray.getJSONObject(i).getString("image_link"));
                        bannerBeanList.add(bb);
                    }
                    if (isViewAttached()) {
                        getView().onSuccessSliderShow(bannerBeanList);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailPackage(meta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            if (isViewAttached()) {
                getView().onFailPackage(response.body().toString());
            }
        }
    }

    /**
     * 获取套餐数据
     */
    private void getPackageImp(int page) {
        System.out.println("==========url========"+NetWorkPath.GET_PROMO_PRODUCT_PACKAGE + "?page=" + page);
        OkGo.<String>get(NetWorkPath.GET_PROMO_PRODUCT_PACKAGE + "?page=" + page)
                .params("page_size", NetWorkPath.PAGE_SIZE)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parsePackage(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorPackage(response.getException().toString());
                        }
                    }
                });
    }

    /**
     * 解析套餐数据
     *
     * @param response
     */
    private void parsePackage(Response<String> response) {

        if (response.isSuccessful()) {
            try {
                JSONObject object = new JSONObject(response.body().toString());
                JSONObject metaObj = object.getJSONObject("meta");
                String code = metaObj.getString("code");
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(code)) {
                    JSONObject extraData = metaObj.getJSONObject("extraData");
                    int pageCount = extraData.getInt("pageCount");
                    List<ProductDetail> productDetailList = gson.fromJson(object.getString("data"), new TypeToken<ArrayList<ProductDetail>>() {
                    }.getType());
                    if (isViewAttached()) {
                        getView().onSuccessPackage(productDetailList, pageCount);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailPackage(metaObj.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if (isViewAttached()) {

                }
            }
        } else {
            if (isViewAttached()) {
                getView().onFailPackage(response.body().toString());
            }
        }
    }

    /**
     * 获取一级分类
     */
    private void getHomeCateImp() {
        OkGo.<String>get(NetWorkPath.GET_HOME_CATE)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseHomeCate(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        LogUtil.i(response.getException().toString());
                        super.onError(response);

                    }
                });
    }

    /**
     * 解析一级分类数据
     */
    private void parseHomeCate(Response<String> response) {
        try {
            JSONObject object = new JSONObject(response.body().toString());
            JSONObject meta = object.getJSONObject("meta");
            if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(meta.getString("code"))) {
                List<Classify> classifyList = new ArrayList<>();
                JSONArray array = object.getJSONArray("data");
                for (int i = 0; i < array.length(); i++) {
                    Classify classify = new Classify();
                    classify.setId(array.getJSONObject(i).getString("id"));
                    classify.setLabel(array.getJSONObject(i).getString("label"));
                    classify.setImageUrl(array.getJSONObject(i).getString("thumbnailUrl"));
                    classify.setIds(array.getJSONObject(i).getString("content"));
                    classifyList.add(classify);
                }

                if (isViewAttached()) {
                    getView().onSuccessHomeCate(classifyList);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
